package com.hengxinguotong.zhihuichengjian.ui.statistics.lift;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.ViewPagerAdapter;
import com.hengxinguotong.zhihuichengjian.ui.BaseActivity;
import com.hengxinguotong.zhihuichengjian.ui.statistics.OnActivityInteractionListener;
import com.hengxinguotong.zhihuichengjian.weekpicker.MonthPickerDialog;
import com.hengxinguotong.zhihuichengjian.weekpicker.WeekPickerDialog;
import com.hengxinguotong.zhihuichengjian.weekpicker.entity.Month;
import com.hengxinguotong.zhihuichengjian.weekpicker.entity.Week;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/3/28
 * <p>
 * 升降机报表
 */

public class LiftFormDataActivity extends BaseActivity implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {
    @BindView(R.id.back_tv)
    HXTextView backTv;
    @BindView(R.id.title_tv)
    HXTextView titleTv;
    @BindView(R.id.week_form_data)
    RadioButton weekFormData;
    @BindView(R.id.month_form_data)
    RadioButton monthFormData;
    @BindView(R.id.search_date_tv)
    HXTextView searchDateTv;
    @BindView(R.id.lift_form_data_vp)
    ViewPager liftFormDataVp;
    @BindView(R.id.date_rg)
    RadioGroup dateRg;
    @BindView(R.id.warning_statistics)
    HXTextView warningStatistics;
    @BindView(R.id.violation_statistics)
    HXTextView violationStatistics;
    @BindView(R.id.work_recycle)
    HXTextView workRecycle;
    @BindView(R.id.bottom_line)
    View bottomLine;

    private int screenWidth,//屏幕宽度
            currentPage;//选中页
    private List<HXTextView> titleList;
    private List<Fragment> fragmentList;
    private ViewPagerAdapter viewPagerAdapter;
    private String startTime,//开始时间
            endTime;//结束时间（月报表时，为空）
    private boolean selectWeek;
    private Week selectedWeek;
    private Month selectedMonth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lift_form_data);
        ButterKnife.bind(this);
        initBottomLineWidth();
        initWeek();
        initView();
    }

    private void initWeek() {
        selectWeek = true;
        selectedWeek = new Week();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        selectedWeek.setWeekBegin(calendar.getTime());
        startTime = selectedWeek.getWeekBeginDate1();
        calendar.add(Calendar.DAY_OF_MONTH, 6);
        selectedWeek.setWeekEnd(calendar.getTime());
        endTime = selectedWeek.getWeekEndDate1();
        searchDateTv.setText(selectedWeek.getSelectWeekBeginAndEnd());
    }

    private void initMonth() {
        selectWeek = false;
        selectedMonth = new Month();
        Calendar calendar = Calendar.getInstance();
        selectedMonth.setYear(calendar.get(Calendar.YEAR));
        selectedMonth.setMonth(calendar.get(Calendar.MONTH) + 1);
        startTime = selectedMonth.getYearAndMonth1();
        endTime = "";
        searchDateTv.setText(selectedMonth.getYearAndMonth3());
    }

    private void initView() {
        titleTv.setText(R.string.lift_form_data);
        dateRg.setOnCheckedChangeListener(this);

        titleList = new ArrayList<>();
        titleList.add(warningStatistics);
        titleList.add(violationStatistics);
        titleList.add(workRecycle);
        currentPage = 0;
        exchange();

        fragmentList = new ArrayList<>();
        fragmentList.add(LiftFormDataWarningFragment.newInstance(startTime, endTime));
        fragmentList.add(LiftFormDataViolationFragment.newInstance(startTime, endTime));
        fragmentList.add(LiftFormDataWorkRecycleFragment.newInstance(startTime, endTime));
        viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList);
        liftFormDataVp.setAdapter(viewPagerAdapter);
        liftFormDataVp.addOnPageChangeListener(this);
        liftFormDataVp.setCurrentItem(currentPage);
    }

    private void exchange() {
        for (int i = 0; i < titleList.size(); i++) {
            if (i == currentPage) {
                titleList.get(i).setTextColor(getResources().getColor(R.color.color_f288ae5));
            } else {
                titleList.get(i).setTextColor(getResources().getColor(R.color.color_666666));
            }
        }
    }

    //初始化底部导航线的长度
    private void initBottomLineWidth() {
        DisplayMetrics me = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(me);
        //得到屏幕宽度的像素点
        screenWidth = me.widthPixels;
        //得到底部导航线的布局参数类对象
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) bottomLine.getLayoutParams();
        //将宽度设置为屏幕宽度的1/3-100
        lp.width = screenWidth / 3 - 100;
        //设置线条边距50
        lp.leftMargin = 50;
        //将布局参数设置回slider
        bottomLine.setLayoutParams(lp);
    }

    @OnClick({R.id.back_tv, R.id.search_date_tv, R.id.warning_statistics, R.id.violation_statistics, R.id.work_recycle})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_tv:
                finish();
                break;
            case R.id.search_date_tv:
                AlertDialog dialog = null;
                if (selectWeek) {
                    dialog = new WeekPickerDialog(LiftFormDataActivity.this, new WeekPickerDialog.OnWeekSelectListener() {
                        @Override
                        public void onWeekSelect(Week week) {
                            if (week == null) {
                                searchDateTv.setText("请选择");
                            } else {
                                selectedWeek = week;
                                searchDateTv.setText(week.getSelectWeekBeginAndEnd());
                                startTime = week.getWeekBeginDate1();
                                endTime = week.getWeekEndDate1();
                                initData();
                            }
                        }
                    });
                } else {
                    dialog = new MonthPickerDialog(LiftFormDataActivity.this, new MonthPickerDialog.OnMonthSelectListener() {
                        @Override
                        public void onMonthSelect(Month month) {
                            if (month == null) {
                                searchDateTv.setText("请选择");
                            } else {
                                selectedMonth = month;
                                searchDateTv.setText(month.getYearAndMonth2());
                                startTime = month.getYearAndMonth1();
                                endTime = "";
                                initData();
                            }
                        }
                    });
                }
                dialog.getWindow().setGravity(Gravity.BOTTOM);
                dialog.show();
                break;
            case R.id.warning_statistics:
                currentPage = 0;
                liftFormDataVp.setCurrentItem(currentPage);
                exchange();
                initData();
                break;
            case R.id.violation_statistics:
                currentPage = 1;
                liftFormDataVp.setCurrentItem(currentPage);
                exchange();
                initData();
                break;
            case R.id.work_recycle:
                currentPage = 2;
                liftFormDataVp.setCurrentItem(currentPage);
                exchange();
                initData();
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getId()) {
            case R.id.date_rg:
                switch (checkedId) {
                    case R.id.week_form_data:
                        initWeek();
                        break;
                    case R.id.month_form_data:
                        initMonth();
                        break;
                }
                break;
        }
        initData();
    }

    private void initData() {
        Fragment fragment = fragmentList.get(currentPage);
        if (fragment instanceof OnActivityInteractionListener) {
            ((OnActivityInteractionListener) fragment).OnActivityInteraction(startTime, endTime);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        //得到bottom_line的LayoutParams参数对象
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) bottomLine.getLayoutParams();
        int marginLeft = 0;
        if (currentPage == position) {
            marginLeft = (int) (currentPage * (screenWidth / 3) + positionOffset * (screenWidth / 3)) + 50;
            //为布局参数的左边距赋值
            lp.leftMargin = marginLeft;
            bottomLine.setLayoutParams(lp);
        } else {
            marginLeft = (int) (currentPage * (screenWidth / 3) - ((1 - positionOffset) * (screenWidth / 3))) + 50;
            lp.leftMargin = marginLeft;
            bottomLine.setLayoutParams(lp);
        }
    }

    @Override
    public void onPageSelected(int position) {
        currentPage = position;
        exchange();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
